package com.yztc.orangeentertainment.ui.main.findfragment.findmain;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.base.BaseFragment;
import com.yztc.orangeentertainment.bean.findbean.HotSearchStarDataBean;
import com.yztc.orangeentertainment.bean.findbean.HotSearchStarListBean;
import com.yztc.orangeentertainment.bean.findbean.HotSearchStarTagBean;
import com.yztc.orangeentertainment.ui.main.findfragment.findadapter.SearchSatrRlvAdapter;
import com.yztc.orangeentertainment.ui.main.findfragment.findpersenter.SearchStarPresenter;
import com.yztc.orangeentertainment.ui.main.findfragment.findutils.FlowLayout;
import com.yztc.orangeentertainment.ui.main.findfragment.findview.ISearchStarView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/9.
 */
public class FindStarSearchFragment extends BaseFragment implements ISearchStarView {
    @InjectView(R.id.findsearchstar_rlv)
    RecyclerView findsearchstarRlv;
    //    @InjectView(R.id.into_tvone)
//    TextView intoTvone;
//    @InjectView(R.id.into_rlv)
//    RecyclerView intoRlv;
//    @InjectView(R.id.putinto_one)
//    LinearLayout putintoOne;
    @InjectView(R.id.into_flowlayout)
    FlowLayout intoFlowlayout;
//    @InjectView(R.id.putinto_two)
//    LinearLayout putintoTwo;


    private SearchStarPresenter presenter;
    private String keyword;

    public FindStarSearchFragment(String keyword) {
        this.keyword = keyword;
    }

    @Override
    public int getLayoutId() {
        return R.layout.findsearch_star;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.inject(this, rootView);
        initData();
        return rootView;
    }

    public void initData() {
        presenter = new SearchStarPresenter(this);
        presenter.getSearchStarListFromNet(keyword);

        findsearchstarRlv.setLayoutManager(new LinearLayoutManager(getActivity()));

//        intoRlv.setLayoutManager(new LinearLayoutManager(getActivity()) {
//            @Override
//            public boolean canScrollVertically() {
//                return false;
//            }
//        });
    }

    @Override
    public void setSearchStarList(HotSearchStarDataBean hotSearchStarDataBean) {
        List<HotSearchStarListBean> hotSearchStarListBeens = hotSearchStarDataBean.getList();
        SearchSatrRlvAdapter adapter = new SearchSatrRlvAdapter(getActivity(),
                hotSearchStarListBeens);
        findsearchstarRlv.setAdapter(adapter);

//        List<HotSearchStarStarBean> starStarBeans = hotSearchStarDataBean.getStar();
//        if (starStarBeans != null) {
//            putintoOne.setVisibility(View.VISIBLE);
//            FindSearchStarInputAdapter inputAdapter = new FindSearchStarInputAdapter(getActivity
//                    (), starStarBeans);
//            intoRlv.setAdapter(inputAdapter);
//        }
//
        List<HotSearchStarTagBean> tagBeans = hotSearchStarDataBean.getTag();
        if (tagBeans != null) {
//            putintoTwo.setVisibility(View.VISIBLE);
            for (int i = 0; i < tagBeans.size(); i++) {
                final TextView tv = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout
                        .findsearch_flowlayout, intoFlowlayout, false);
                tv.setText(tagBeans.get(i).getName());
                final String str = (String) tv.getText();
                //点击事件
                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        FindSearchActivity.findsearchEdt.setText(str);
                    }
                });
                intoFlowlayout.addView(tv);//添加到父View
            }
        }
    }

    @Override
    public void onVerticalFail(String msg) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
